var app = getApp();
// var d = require("../../../../../../template/calendar/calendar.js");
Page({


  /**
   * 页面的初始数据
   */
  data: {
    active: true,
    showCalendar: false,
    year: 0,
    month: 0,
    isToday: 0,
    isTodayWeek: false,
    todayIndex: 0,
    time: '',
    selectVal: '',
    typeindex: false,
    proIndex: 0,
    proshow: false,
    cityname: '',
    money: 0,
    count: 0,
    total: 0,
    arr: [],
    addPackageList: [],
    listcity: [],
    tcProject: [],
    addproject: []
  },
  calenderOpen: function () {
    this.setData({
      showCalendar: !this.data.showCalendar
    })
  },
  closeMask: function () {
    this.setData({
      showCalendar: !this.data.showCalendar
    })
  },
  select(e) {
    console.log(e)
    var that = this
    app.util.request({
      'url': 'entry/wxapp/Tijian.timemumenone',
      data: {
        chooseDate: e.detail
      },
      success: function (res) {
        console.log(res)
        that.setData({
          typeindex: true,
          week: res.data.week,
          listcity: res.data.data
        })
      }
    })
    that.setData({
      selectVal: e.detail,
      showCalendar: !this.data.showCalendar
    })
    var selectVal = this.data.selectVal
    var time = selectVal.substring(5, selectVal.length)
    that.setData({
      time: time
    })
    console.log(time)
  },
  goToCheckItemsDetail: function () {
    var that = this
    var detail = JSON.stringify(that.data.detail)
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/tijian/mealCont/mealCont?detail=' + detail,
    })
  },
  showInfo: function (e) {
    var name = e.currentTarget.dataset.itemName
    var cont = e.currentTarget.dataset.itemIntro
    wx.showModal({
      title: name,
      content: cont,
      showCancel: false,
      confirmText: '确定',
      confirmColor: '#fd965b',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  getCalendarData: function (e) {

  },
  //选择
  handleTapView: function (e) {
    var index = e.currentTarget.dataset.index
    var addPackageList = this.data.addPackageList;
    addPackageList[index].checked = !addPackageList[index].checked
    var total = this.data.total
    // total = parseInt(total)
    console.log(total)
    var count = this.data.count
    var addproject = []
    for (let i = 0; i < addPackageList.length; i++) {
      if (addPackageList[i].checked) {
        total = total + addPackageList[i].salePrice
        console.log(addPackageList[i].salePrice)
        addproject.push(addPackageList[i])
        var num = addproject.length;
        console.log(num)
        count = parseInt(num) + parseInt(count)
        console.log(count)
        console.log(total)

      } else {
        total = total - addPackageList[i].salePrice
        addproject.splice(index, 1)
        console.log(addproject)
        var num = addproject.length;
        console.log(num)
        count = count - 1
        console.log(count)
        console.log(total)
      }
    }
    console.log(addproject)
    this.setData({
      count: count,
      total: total,
      addproject: addproject
    })
    // console.log(e)
    // var index = e.currentTarget.dataset.index
    // var subIndex = e.currentTarget.dataset.subindex
    // var total = this.data.total
    // // total = parseInt(total)
    // var addPackageList = this.data.addPackageList;
    // console.log(addPackageList)
    // var count = this.data.count
    // var arr = this.data.arr
    // console.log(arr)
    // // console.log(addPackageList[index].items[subIndex]);
    // addPackageList[index].checked = !addPackageList[index].checked
    // this.setData({
    //   addPackageList: addPackageList
    // })
    // console.log(addPackageList)
    // if (addPackageList[index].checked == true) {
    //   total += addPackageList[index].salePrice
    //   var list = {
    //     itemName: addPackageList[index].y_msg,
    //     itemprice: addPackageList[index].salePrice

    //   }
    //   console.log(list)
    //   console.log(total)
    //   arr.push(list)
    //   count++

    // } else {
    //   total -= addPackageList[index].salePrice
    //   arr.splice(index, 1)
    //   count--
    // }
    // this.setData({
    //   total: total,
    //   count: count,
    //   arr: arr
    // })
    // console.log(arr)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    console.log(options)
    var tcProject = JSON.parse(options.project)
    console.log(tcProject)
    var that = this
    let now = new Date();
    var detail = JSON.parse(decodeURIComponent(options.detail))

    console.log(detail)
    var addPackageList = detail.y_msg
    for (var i = 0; i < addPackageList.length; i++) {
      addPackageList[i].salePrice = parseFloat(addPackageList[i].salePrice)
    }
    console.log(addPackageList)
    let year = now.getFullYear();
    let month = now.getMonth() + 1;
    var count = options.count
    var money = options.money
    var title = options.title

    var city = wx.getStorageSync('city')

    that.setData({
      year: year,
      month: month,
      today: now.getDate(),
      isToday: '' + year + month + now.getDate(),
      count: count,
      title: title,
      money: parseFloat(money),
      cityname: city,
      detail: detail,
      addPackageList: addPackageList,
      // arr:arr,
      tcProject: tcProject
    })
    console.log(that.data.today)
    var tid = options.tid
    that.setData({
      tid: tid
    })

    //请求 后三天的 时间 周
    app.util.request({
      'url': 'entry/wxapp/Tijian.timelist',
      data: {
        q: 1
      },
      success: function (res) {
        console.log(res)
        var num = 0
        var list_wedate = res.data
        var time_in = list_wedate[0]['date']
        for (var i = 0; i < list_wedate.length; i++) {
          var date = list_wedate[i].date
          var time = date.substring(5, date.length)
          list_wedate[i].date = time
          if ([i] == 0) {
            list_wedate[i].active = true
            list_wedate[i].num = 0
          } else {
            list_wedate[i].active = false
            list_wedate[i].num = 0
          }
        }
        that.setData({
          list_wedate: list_wedate,
          time_in: time_in
        })
        var num = 0;
        var list_wedate = that.data.list_wedate
        for (var i = 1; i < list_wedate.length; i++) {
          num = num + 100
          list_wedate[i].num = num
        }
        that.setData({
          list_wedate: list_wedate
        })
        var date = list_wedate[0].date
        var time = list_wedate[0].time
        var chooseDate = time + '-' + date
        app.util.request({
          'url': 'entry/wxapp/Tijian.timemumen',
          data: {
            chooseDate: chooseDate
          },
          success: function (res) {
            console.log(res)
            that.setData({
              listcity: res.data,
              chooseDate: chooseDate
            })
          }
        })
      }
    })
    app.util.request({
      'url': 'entry/wxapp/Hzbingli.url',
      success: function (res) {
        console.log(res)
        that.setData({
          url: res.data,
        })
      }
    })
    console.log(that.data.listcity)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let pages = getCurrentPages();
    let currPage = pages[pages.length - 1];
    if (currPage.data.cityname) {
      this.setData({
        cityname: currPage.data.cityname,
        id: currPage.data.id
      })
    }
    if (currPage.data.names) {
      console.log(currPage.data)
      this.setData({
        names: currPage.data.names,
        tel: currPage.data.tel,
        j_id: currPage.data.j_id,
        numcard: currPage.data.numcard,
        sex: currPage.data.sex
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  changeDate(e) {
    console.log(e)
    var that = this
    var index = e.currentTarget.dataset.index
    var list_wedate = that.data.list_wedate
    var chooseDate = e.currentTarget.dataset.chooseDate
    for (var i = 0; i < list_wedate.length; i++) {
      if ([i] == index) {
        list_wedate[i].active = true
      } else {
        list_wedate[i].active = false
      }
    }
    that.setData({
      list_wedate: list_wedate
    })
    app.util.request({
      'url': 'entry/wxapp/Tijian.timemumen',
      data: {
        chooseDate: chooseDate
      },
      success: function (res) {
        console.log(res)
        that.setData({
          listcity: res.data,
          time_in: chooseDate
        })
      }
    })
  },
  changeDateweek() {
    var that = this
    //请求 后三天的 时间 周
    app.util.request({
      'url': 'entry/wxapp/Tijian.timelist',
      data: {
        q: 1
      },
      success: function (res) {
        var num = 0
        var list_wedate = res.data
        for (var i = 0; i < list_wedate.length; i++) {
          var date = list_wedate[i].date
          var time = date.substring(5, date.length)
          list_wedate[i].date = time
          if ([i] == 0) {
            list_wedate[i].active = true
            list_wedate[i].num = 0
          } else {
            list_wedate[i].active = false
            list_wedate[i].num = 0
          }
        }
        that.setData({
          list_wedate: list_wedate
        })
        var num = 0;
        var list_wedate = that.data.list_wedate
        for (var i = 1; i < list_wedate.length; i++) {
          num = num + 100
          list_wedate[i].num = num
        }
        that.setData({
          list_wedate: list_wedate
        })
        var date = list_wedate[0].date
        var time = list_wedate[0].time
        var chooseDate = time + '-' + date
        app.util.request({
          'url': 'entry/wxapp/Tijian.timemumen',
          data: {
            chooseDate: chooseDate
          },
          success: function (res) {
            console.log(res)
            that.setData({
              listcity: res.data,
              typeindex: false
            })
          }
        })
      }
    })
  },
  chooseCityTab() {
    var that = this
    wx.navigateTo({
      url: '/hyb_yl/userLife/pages/tijian/choose-city/choose-city',
    })
  },
  goAddon(e) {
    var money = this.data.money
    var chooseDate = this.data.chooseDate
    var time_in = this.data.time_in
    var jg_name = e.currentTarget.dataset.jg_name
    var bm_id = e.currentTarget.dataset.j_id //部门id
    this.setData({
      proIndex: 1,
      proshow: !this.data.proshow,
      total: this.data.money,
      jg_name: jg_name,
      time_in: time_in,
      bm_id: bm_id
    })
  },
  // 返回上一步
  goPre: function () {
    // var arr = this.data.arr
    var addPackageList = this.data.addPackageList
    var addproject = this.data.addproject
    var arr = []
    for (var i = 0; i < addPackageList.length; i++) {
      addPackageList[i].checked = false
    }
    var count = this.data.count;
    count = parseInt(count) - parseInt(addproject.length)
    addproject = []
    this.setData({
      proIndex: 0,
      addPackageList: addPackageList,
      count: count,
      addproject: addproject,
      arr: arr
    })
  },
  // 跳转到立即支付
  goNext: function () {
    var arr = this.data.arr
    var tcProject = this.data.tcProject
    var addPackageList = this.data.addPackageList
    console.log(addPackageList)
    var list = {
      itemName: this.data.title,
      itemprice: parseFloat(this.data.money),
      tcProject: tcProject,
    }
    console.log(list)
    arr.push(list)
    console.log(arr)

    this.setData({
      proIndex: 2,
      arr: arr
    })
  },
  chooseContact() {
    wx.navigateTo({
      url: '/hyb_yl/zhuanjiasubpages/pages/huanzhexinxi/huanzhexinxi',
    })
  },
  // 立即支付
  nowpay(e) {
    var that = this
    var arr = that.data.arr //套餐数据
    var content = arr[0].tcProject
    var addproject = this.data.addproject //加包项
    console.log(addproject)
    var money = parseFloat(e.currentTarget.dataset.money)
    var bm_id = that.data.bm_id
    var j_id = that.data.j_id
    var uniacid = app.siteInfo.uniacid
    var tid = that.data.tid
    console.log(bm_id, arr, money)
    //支付
    wx.showModal({
      content: '是否支付',
      success: function (res) {
        if (res.confirm) {

          app.util.request({
            'url': that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Pay&m=hyb_yl',
            header: {
              "Content-Type": "application/xml"
            },
            method: "GET",
            data: {
              openid: wx.getStorageSync('openid'),
              z_tw_money: money
            },
            success: function (res) {
              console.log(res);
              wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr, //字符串随机数
                'package': res.data.package,
                'signType': res.data.signType,
                'paySign': res.data.paySign,
                success: function (res) {
                  //跳转订单页
                  app.util.request({
                    'url': 'entry/wxapp/Tijian.addtijian',
                    data: {
                      content: content,
                      money: money,
                      j_id: j_id,
                      bm_id: bm_id,
                      ifpay: 1,
                      yy_time: that.data.time_in,
                      tid: tid,
                      openid: wx.getStorageSync('openid'),
                      addproject: addproject
                    },
                    success: function (res) {
                      console.log(res)
                      wx.showToast({
                        title: '支付成功',
                        icon: 'none',
                        duration: 1500,
                        success: function (res) {
                          wx.redirectTo({
                            url: '/hyb_yl/mysubpages/pages/physicalOrder/physicalOrder',
                          })
                        }
                      })
                    }
                  })
                },
                fail: function () {
                  app.util.request({
                    'url': 'entry/wxapp/Tijian.addtijian',
                    data: {
                      content: content,
                      money: money,
                      j_id: j_id,
                      bm_id: bm_id,
                      ifpay: 0,
                      yy_time: that.data.time_in,
                      tid: tid,
                      openid: wx.getStorageSync('openid'),
                      addproject: addproject
                    },
                    success: function (res) {
                      console.log(res)
                      wx.redirectTo({
                        url: '/hyb_yl/mysubpages/pages/physicalOrder/physicalOrder',
                      })
                    }
                  })
                }
              })
            }
          })
        } else {
          app.util.request({
            'url': 'entry/wxapp/Tijian.addtijian',
            data: {
              content: content,
              money: money,
              j_id: j_id,
              bm_id: bm_id,
              ifpay: 0,
              yy_time: that.data.time_in,
              tid: tid,
              openid: wx.getStorageSync('openid'),
              addproject: addproject
            },
            success: function (res) {
              console.log(res)
              wx.redirectTo({
                url: '/hyb_yl/mysubpages/pages/physicalOrder/physicalOrder',
              })
            }
          })
        }
      }

    })


  }

  // 第三部分返回上一步
  // nowback:function(){
  //   this.setData({
  //     proIndex:1,
  //   })
  // }
})